<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.id"
        placeholder="ID"
        style="width: 200px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.mobile"
        placeholder="手机"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.name"
        placeholder="名称"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-input
        v-model="listQuery.shop"
        placeholder="店铺"
        style="width: 200px; margin-left: 10px"
        class="filter-item"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-button
        v-waves
        type="primary"
        icon="el-icon-search"
        class="filter-item"
        style="margin-left: 10px;"
        @click="handleFilter"
      >搜 索</el-button>
      <el-button
        v-waves
        type="success"
        icon="el-icon-refresh"
        class="filter-item"
        style="margin-left: 10px;"
        @click="clearFilter"
      >清除筛选</el-button>
    </div>
    <div class="filter-container" v-if="false">
      <el-button
        v-waves
        type="primary"
        icon="el-icon-circle-plus-outline"
        class="filter-item"
        size="small"
        @click="handleCreate"
      >添加</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column sortable="custom" prop="id" label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员ID" v-if="false">
        <template slot-scope="{row}">
          <span>{{ row.user_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机" width="120">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺">
        <template slot-scope="{row}">
          <span>{{ row.shop }}</span>
        </template>
      </el-table-column>
      <el-table-column label="城市">
        <template slot-scope="{row}">
          <span>{{ row.city }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址">
        <template slot-scope="{row}">
          <span>{{ row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库" v-if="false">
        <template slot-scope="{row}">
          <span v-if="row.warehouse">{{row.warehouse.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设置仓库" v-if="false">
        <template slot-scope="{row}">
          <el-select
            v-model="row.warehouse_id"
            class="filter-item"
            filterable
            @change="handleWareHouse(row)"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.id"
              :label="`${item.name}`"
              :value="item.id"
              style="color: red"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="handleOrder(row)">
            代客下单
          </el-button>
          <el-divider v-if="false" />
          <el-button type="success" size="mini" @click="handleUpdate(row)" v-if="false">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)" v-if="false">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      @pagination="refresh"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import api from '@/api'
import { oldRemove } from '@/api/modules/member'

const initQuery = {
  page: 1,
  pageSize: 10,
  total: 0,
  sort: '+id'
}

export default {
  name: 'MemberList',
  components: {
    Pagination,
  },
  directives: { waves },
  filters: {
    timeFilter(time) {
      return time ? parseTime(time, '{m}-{d} {h}:{i}') : ''
    },
  },
  data() {
    return {
      list: [],
      listQuery: {},
      defaultSort: {},
      selectionList: [],
      listLoading: false,
      warehouseList: []
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    // this.getWarehouse()
    this.getList()
  },
  methods: {
    handleDelete(row) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.member.oldRemove({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '删除成功',
            type: 'success'
          })
          this.list.forEach(x => {
            if (x.id === row.id) {
              x.status = -99
            }
          })
          // 删除掉
          this.list = this.list.filter(x => x.id !== row.id)
        })
      }).catch(() => {})
    },
    handleCreate() {
      this.$router.push({
        path: '/member/old-create'
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: `/member/old-edit/${row.id}`
      })
    },
    // 指定仓库
    handleWareHouse(row) {
      api.member.setWarehouseOld({ ...row }).then(() => {
        this.$message.success({
          message: '设置成功'
        })
      })
    },
    // 仓库列表
    async getWarehouse() {
      const response = await api.member.houseList()
      this.warehouseList = response.data.list
      // console.log(this.warehouseList)
    },
    /**
     * 获取数据列表
     */
    getList() {
      this.listLoading = true
      api.member.old({ ...this.listQuery }).then(response => {
        const { data } = response
        this.list = data.list
        this.listQuery.total = +data.totalCount
        this.listLoading = false
      }).finally(() => {
        this.listLoading = false
      })
    },
    /**
     * 修改
     * @param row
     */
    handleOrder(row) {
      this.$router.push({
        // path: `/member/bao/${row.id}`
        path: `/marketing/bao/${row.id}`
      })
    },
    /**
     * 排序
     * @param data
     */
    sortChange(data) {
      const sortBy = (prop, order) => {
        if (prop && order === 'ascending') {
          this.listQuery.sort = `+${prop}`
        } else if (prop && order === 'descending') {
          this.listQuery.sort = `-${prop}`
        } else {
          this.listQuery.sort = ''
        }
      }
      const { prop, order } = data
      sortBy(prop, order)
      this.handleFilter()
    },
    /**
     * 刷新
     */
    refresh() {
      this.$router.push({
        // path: '/member/old',
        path: '/marketing/order',
        query: this.listQuery
      })
    },
    /**
     * 过滤(筛选)
     */
    handleFilter() {
      // this.listQuery.page = 1
      this.refresh()
    },
    /**
     * 清除筛选
     */
    clearFilter() {
      this.listQuery = Object.assign({}, initQuery)
      this.defaultSort = {}
      this.refresh()
    },
    /**
     * 处理筛选条件
     */
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      let sort = '-id'
      this.listQuery = Object.assign({}, { ...initQuery, sort })
      if (Object.keys(query).length > 0) {
        // 注意这里的类型转换
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.total && (query.total = +query.total)
        query.sort && (sort = query.sort)
      }
      const sortSymbol = sort[0]
      const sortColumn = sort.slice(1, sort.length)
      this.defaultSort = {
        prop: sortColumn,
        order: sortSymbol === '+' ? 'ascending' : 'descending'
      }
      this.listQuery = { ...this.listQuery, ...query }
    },
  },
  /**
   * 刷新
   * @param to
   * @param from
   * @param next
   */
  beforeRouteUpdate(to, from, next) {
    if (to.path === from.path) {
      const newQuery = Object.assign({}, to.query)
      const oldQuery = Object.assign({}, from.query)
      if (JSON.stringify(newQuery) !== JSON.stringify(oldQuery)) {
        this.getList()
      }
    }
    next()
  }
}
</script>

<style scoped lang="scss">
.el-divider--horizontal {
  margin: 7.5px 0;
}
.account {
  text-align: left;
  list-style: none;
  padding: 0;
  li {
    display: block;
  }
}
</style>
